package com.bbzn.user.client.service.impl;

import com.bbzn.user.client.dao.role.RolePermissionMapper;
import com.bbzn.user.client.dto.permission.PermissionDTO;
import com.bbzn.user.client.utils.BabaBeanUtils;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

import com.bbzn.user.client.dao.permission.PermissionMapper;
import com.bbzn.user.client.dataobject.Permission;
import com.bbzn.user.client.service.PermissionService;

@Service
public class PermissionServiceImpl implements PermissionService {

    @Resource
    private PermissionMapper permissionMapper;

    @Resource
    private RolePermissionMapper rolePermissionMapper;

    @Override
    public int deleteByPrimaryKey(Long id) {
        return permissionMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(Permission record) {
        return permissionMapper.insert(record);
    }

    @Override
    public int insertOrUpdate(Permission record) {
        return permissionMapper.insertOrUpdate(record);
    }

    @Override
    public int insertOrUpdateSelective(Permission record) {
        return permissionMapper.insertOrUpdateSelective(record);
    }

    @Override
    public int insertSelective(Permission record) {
        return permissionMapper.insertSelective(record);
    }

    @Override
    public Permission selectByPrimaryKey(Long id) {
        return permissionMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(Permission record) {
        return permissionMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(Permission record) {
        return permissionMapper.updateByPrimaryKey(record);
    }

    @Override
    public List<Permission> findByAll(Permission permission) {
        return permissionMapper.findByAll(permission);
    }

    @Override
    public List<Permission> getNavigationBar(Long id) {
        return permissionMapper.getNavigationBar(id);
    }

    @Override
    public int updateBatch(List<Permission> list) {
        return permissionMapper.updateBatch(list);
    }

    @Override
    public int updateBatchSelective(List<Permission> list) {
        return permissionMapper.updateBatchSelective(list);
    }

    @Override
    public int batchInsert(List<Permission> list) {
        return permissionMapper.batchInsert(list);
    }

    @Override
    public List<Permission> selectByUserId(Long userId) {
        return permissionMapper.selectByUserId(userId);
    }

    @Override
    public List<PermissionDTO> selectTreeByUserId(Long userId) {
        List<Permission> permissions = permissionMapper.selectByUserId(userId);
        return getK(permissions);
    }

    @Override
    public List<PermissionDTO> getRolePermission(int roleId, Long userId) {
        // 获取角色拥有的权限id
        List<Long> p = rolePermissionMapper.findPermissionIdByRoleId(roleId);
        // 获取当前用户对应的角色的资源权限
        List<PermissionDTO> k = permissionMapper.findByAllPermissionByUserId(userId);

        //筛选+
        List<PermissionDTO> pk = k.stream().map(
                x -> {
                    if(p.contains(x.getId())){
                        x.setSign(true);
                        return x;
                    }
                    return x;
                }
        ).collect(Collectors.toList());
        return getV(pk);
    }

    @Override
    public int getMaxSort() {
        return permissionMapper.getMaxSort();
    }

    @Override
    public int batchDelete(List<Long> ids) {
        return permissionMapper.batchDelete(ids);
    }

    @Override
    public List<Long> getPermissions() {
        return permissionMapper.getPermissions();
    }


    /**
     * 所有资源,按顺序显示
     * 显示出树状
     *
     * @return
     */
    public List<PermissionDTO> getK(List<Permission> permissions) {
        //JDK8的stream处理,把根分类区分出来
        List<PermissionDTO> roots = permissions.stream().filter(dealCategory -> (dealCategory.getPid() == 0)).
                map(p -> {
                    PermissionDTO node = BabaBeanUtils.voToDto(p, PermissionDTO.class);
                    return node;
                }).collect(Collectors.toList());
        //对根分类进行排序
        roots.sort(new Comparator<PermissionDTO>() {
            @Override
            public int compare(PermissionDTO o1, PermissionDTO o2) {
                return o1.getId() > o2.getId() ? 1 : -1;
            }
        });

        //把非根分类区分出来
        List<PermissionDTO> subs = permissions.stream().filter(dealCategory -> (dealCategory.getPid() != 0)).map(p -> {
            PermissionDTO subNode = BabaBeanUtils.voToDto(p, PermissionDTO.class);
            return subNode;
        }).collect(Collectors.toList());

        //递归构建结构化的分类信息
        roots.forEach(root -> buildSubs(root, subs));
        return roots;
    }

    /**
     * 所有资源,按顺序显示
     * 显示出树状
     *
     * @return
     */
    public List<PermissionDTO> getV(List<PermissionDTO> permissions) {
        //JDK8的stream处理,把根分类区分出来
        List<PermissionDTO> roots = permissions.stream().filter(dealCategory -> (dealCategory.getPid() == 0)).collect(Collectors.toList());
        //对根分类进行排序
        roots.sort(new Comparator<PermissionDTO>() {
            @Override
            public int compare(PermissionDTO o1, PermissionDTO o2) {
                return o1.getId() > o2.getId() ? 1 : -1;
            }
        });

        //把非根分类区分出来
        List<PermissionDTO> subs = permissions.stream().filter(dealCategory -> (dealCategory.getPid() != 0)).collect(Collectors.toList());

        //递归构建结构化的分类信息
        roots.forEach(root -> buildSubs(root, subs));
        return roots;
    }

    /**
     * 递归构建
     *
     * @param parent
     * @param subs
     */
    private void buildSubs(PermissionDTO parent, List<PermissionDTO> subs) {
        List<PermissionDTO> children = subs.stream().filter(sub -> (sub.getPid().equals(parent.getId()))).collect(Collectors.toList());
        if (!CollectionUtils.isEmpty(children)) {//有子分类的情况
            parent.setChildren(children);
            children.forEach(child -> buildSubs(child, subs));//再次递归构建
        }
    }
}


